package com.wy.business.assembly.actions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.apache.tools.ant.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.assembly.pojos.UploadInfo;
import com.wy.business.assembly.services.UploadInfoService;
import com.wy.component.dataConvert.JsonUtil;
import com.wy.framework.actions.AbstractBaseAction;

/**
 * 
 * 项目名称：openportal_coding
 * 类名称：UploadInfoAction
 * 类描述：文件上传服务
 * 创建人：黄忠
 * 创建时间：2016-5-10 下午1:50:38
 * 修改人：黄忠
 * 修改时间：2016-5-10 下午1:50:38
 * 版本信息：@version 1.0
 * Copyright (c) 2016万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version
 */
@ParentPackage("json")
@Namespace("/file/uploadFile")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class UploadInfoAction extends AbstractBaseAction {

	private static final long serialVersionUID = -5347764846090465650L;
	
	private static Log logger = LogFactory.getLog(UploadInfoAction.class);
	
	@Autowired
	private UploadInfoService uploadInfoService;
	
	private Map<String,String> resultMap = new HashMap<String,String>();
	
	private List<Map> rows = new ArrayList<Map>();
	
	/**
	 * 
	 * Function Name               upload                                   
	 * @return          		             
	 * @description 统一上传图片接口
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-5-10 下午1:50:43           黄忠                     Initial
	 **********************************************************************
	 */
	@Action(value = "uploadImg", 
	interceptorRefs = { @InterceptorRef("sessionStack") }, 
	results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String uploadImg() {
		String uploadInfoJson = ServletActionContext.getRequest().getParameter("uploadInfoJson");
		logger.info("Enter uploadInfoService upload. uploadInfo : "+uploadInfoJson);
		
		UploadInfo uploadInfo = (UploadInfo) JsonUtil.getDTO(uploadInfoJson,UploadInfo.class);
		uploadInfo.setFileType("0");
		try {
			resultMap = uploadInfoService.upload(uploadInfo);
			String errorCode = resultMap.get("error");
			if("001".equals(errorCode)) {
				rspCode = "001001";
				rspMsg = "入参对象为空。";
			} else if ("002".equals(errorCode)) {
				rspCode = "001002";
				rspMsg = "入参对象文件类型为空。";
			}else if ("003".equals(errorCode)) {
				rspCode = "001003";
				rspMsg = "入参对象图片为空。";
			}else if ("004".equals(errorCode)) {
				rspCode = "001004";
				rspMsg = "入参对象图片大小超过限制。";
			}else{
				rspCode = "000000";
				rspMsg = "成功上传。";
			}
			rows.add(resultMap);
		} catch (Exception e) {
			logger.info("UploadInfoAction upload 异常，"+e);
			rspCode = "999999";
			rspMsg = "系统异常。"+e.getMessage();
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * Function Name               upload                                   
	 * @return          		             
	 * @description 统一上传文件接口
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-5-10 下午1:50:43           黄忠                     Initial
	 **********************************************************************
	 */
	@Action(value = "uploadFile", 
	interceptorRefs = { @InterceptorRef("sessionStack") }, 
	results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String uploadFile() {
		
		MultiPartRequestWrapper req = (MultiPartRequestWrapper) ServletActionContext.getRequest();
		
		// 文件名
		if(req.getFileNames("file") == null) {
			rspCode = "001005";
			rspMsg = "入参对象文件为空。";
			logger.info("UploadInfoAction uploadFile 入参对象文件为空");
			return SUCCESS;
		}
		
		String fileName = req.getFileNames("file")[0];
		logger.info("UploadInfoAction uploadFile fileName " + fileName);
    	File file = req.getFiles("file")[0];
    	UploadInfo uploadInfo = new UploadInfo();
    	uploadInfo.setFileType("1");
    	uploadInfo.setOtherFile(file);
    	uploadInfo.setFileName(fileName);
		try {
			resultMap = uploadInfoService.upload(uploadInfo);
			String errorCode = resultMap.get("error");
			if ("005".equals(errorCode)) {
				rspCode = "001005";
				rspMsg = "入参对象文件内容为空。";
			}else if ("006".equals(errorCode)) {
				rspCode = "001001";
				rspMsg = "入参对象文件大小超过限制。";
			}else{
				rspCode = "000000";
				rspMsg = "成功上传。";
			}
			rows.add(resultMap);
		} catch (Exception e) {
			rspCode = "999999";
			rspMsg = "系统异常。"+e.getMessage();
			logger.info("UploadInfoAction upload 异常，"+e);
		}

		return SUCCESS;
	}

	public Map<String, String> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, String> resultMap) {
		this.resultMap = resultMap;
	}

	public List<Map> getRows() {
		return rows;
	}

	public void setRows(List<Map> rows) {
		this.rows = rows;
	}
	
	
	
	
	
	

}
